package com.glory.dgcpcpda.activity.cpc;

import android.os.Build;
import android.os.Handler;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.adapter.NewWForMaterialListAdapter;
import com.glory.dgcpcpda.adapter.WForMaterialListAdapter;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.base.Constant;
import com.glory.dgcpcpda.entity.AdrefListEntity;
import com.glory.dgcpcpda.entity.ChangeShiftEntity;
import com.glory.dgcpcpda.entity.EqpInfoEntity;
import com.glory.dgcpcpda.entity.EywEqpUploadWaitEntity;
import com.glory.dgcpcpda.entity.StripMappingEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.LotUtils;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class NewWForMaterialActivity extends BaseActivity {
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.rvPEListS)
    RecyclerView rvPEList;

    @BindView(R.id.list_tv1)
    TextView listTv1;
    @BindView(R.id.list_tv2)
    TextView listTv2;
    @BindView(R.id.list_tv3)
    TextView listTv3;
    @BindView(R.id.list_tv4)
    TextView listTv4;
    @BindView(R.id.list_tv5)
    TextView listTv5;
    @BindView(R.id.list_tv6)
    TextView listTv6;

    @BindView(R.id.tvEqpFloor)
    TextView tvEqpFloor;
    @BindView(R.id.ivEqpTypeArrow)
    ImageView ivEqpTypeArrow;
    @BindView(R.id.flEqpTypeList)
    FrameLayout flEqpTypeList;

    private List<AdrefListEntity.Adref>  adreflist = new ArrayList<>();
    private List<String>  mEqpFloor = new ArrayList<>();
    private ArrayList<EywEqpUploadWaitEntity> mUploadWait = new ArrayList<>();
    private FreeUI_GeneralPop mEqpTypePop;
    private String Count="";
    private NewWForMaterialListAdapter adapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_cpc_newwformaterial;
    }

    @Override
    protected void initView() {
        tvTitle.setText("待料查询");
        setMainLayout(llMain);
        initRecyclerView(rvPEList, this);
        adapter = new NewWForMaterialListAdapter(null,this);
        adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        adapter.isFirstOnly(true);
        rvPEList.setAdapter(adapter);

        listTv1.setText("区域标识");
        listTv2.setText("机台号");
        listTv3.setText("封装形式");
        listTv4.setText("配线图号");
        listTv5.setText("操作");
        listTv6.setText("");
        tvBaseSubmit.setText("关闭");
        listTv5.setVisibility(View.VISIBLE);
        listTv6.setVisibility(View.VISIBLE);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) listTv6.getLayoutParams();
        params.weight = 0.5f;
        listTv6.setLayoutParams(params);

//        requestFloorList();
        requestMaterialList("");
    }

//    private void requestFloorList() {
//        //获取MES动态栏位参考值
//            FreeUI_EntityCallBack<BaseResponseBean<AdrefListEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<AdrefListEntity>>
//                    (new TypeToken<BaseResponseBean<AdrefListEntity>>() {
//                    }.getType(), getSupportFragmentManager(), this) {
//
//                @RequiresApi(api = Build.VERSION_CODES.N)
//                @Override
//                public void onSuccess(Response<BaseResponseBean<AdrefListEntity>> response) {
//                    super.onSuccess(response);
//                    if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())){
//                        adreflist = response.body().getResponse().getBody().getADREFLIST();
//                        if(adreflist.size()>0){
//                            for(int i=0;i<adreflist.size();i++)
//                            {
//                                mEqpFloor.add(adreflist.get(i).getDESCRIPTION());
//                            }
//                        }
//                    }
//                }
//
//                @Override
//                public void onError(Response<BaseResponseBean<AdrefListEntity>> response) {
//                    super.onError(response);
//                    loadError(response.getException(),"ADREFLISTQUERY");
//
//                }
//            };
//            JsonObject map = new JsonObject();
//            map.addProperty("REFERENCETYPE","USER");
//            map.addProperty("REFERENCENAME","EquipmentAera");
//            NetUtils.requestPostNet("CPC.ADREFLISTQUERY","ADREFLISTQUERY",this,map,callBack);
//    }

    private void requestMaterialList(String floor) {
        FreeUI_EntityCallBack<BaseResponseBean<ArrayList<EywEqpUploadWaitEntity>>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<ArrayList<EywEqpUploadWaitEntity>>>
                (new TypeToken<BaseResponseBean<ArrayList<EywEqpUploadWaitEntity>>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<ArrayList<EywEqpUploadWaitEntity>>> response) {
                super.onSuccess(response);
                mUploadWait.clear();
                mUploadWait = response.body().getResponse().getBody();
                adapter.setNewData(mUploadWait);
                if(floor.equals("")){
                    for(EywEqpUploadWaitEntity item : mUploadWait){
                        if(!mEqpFloor.contains(item.getPLACE())){
                            mEqpFloor.add(item.getPLACE());
                        }
                    }
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<ArrayList<EywEqpUploadWaitEntity>>> response) {
                super.onError(response);
                loadError(response.getException(),"GETMATERIALLIST");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("PLACE",floor);
        NetUtils.requestPostNet("CPC.GETMATERIALLIST","GETMATERIALLIST",this,map,callBack);
    }
    private void requestCloseMaterialList(EywEqpUploadWaitEntity item) {
        FreeUI_EntityCallBack<BaseResponseBean<EywEqpUploadWaitEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<EywEqpUploadWaitEntity>>
                (new TypeToken<BaseResponseBean<EywEqpUploadWaitEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<EywEqpUploadWaitEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())){
                    showToast("关闭成功",true);
                    adapter.deleteData.clear();
                    requestMaterialList(tvEqpFloor.getText().toString());
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<EywEqpUploadWaitEntity>> response) {
                super.onError(response);
                loadError(response.getException(),"PendingMaterialReporting");
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("Id",item.getID());
        map.addProperty("REPORTNAME","关闭");
        map.addProperty("REPORTUSER", Constant.CURRENT_USER);
        NetUtils.requestPostNet("CPC.PendingMaterialReporting","PendingMaterialReporting",this,map,callBack);
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.tvBaseSubmit,R.id.flEqpTypeList})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.tvBaseSubmit:
                if (adapter.deleteData.size() <= 0) {
                    showToast("请勾选信息后，在进行关闭操作",false);
                    return;
                }
                showSubmitDialog(new onSubmitListener() {
                    @Override
                    public void setOnSubmitListener() {
                        for(EywEqpUploadWaitEntity item : adapter.deleteData)
                        {
                            new Handler().postDelayed(() ->requestCloseMaterialList(item) , 500);
                        }
                    }
                },"是否确认关闭");
                break;
            case R.id.flEqpTypeList:
                if (mEqpFloor.size() <= 0) {
                    showToast("未获取到设备区域列表", false);
                    return;
                }

                if (mEqpTypePop != null) {
                    if (mEqpTypePop.isShowing()) {
                        mEqpTypePop.dismiss();
                    }
                } else {
                    mEqpTypePop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flEqpTypeList.getWidth(), mEqpFloor, ivEqpTypeArrow, (codeS, i) -> {
                        tvEqpFloor.setText(codeS);
                        requestMaterialList(codeS);
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivEqpTypeArrow);
                mEqpTypePop.showAsDropDown(flEqpTypeList, 0, 0);
                break;
        }

    }

}
